package com.wo.android.pockybill;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.wo.android.pockybill.ui.NotifyComponet;
import com.wo.android.pockybill.ui.NotifyComponetImpl;
import com.wow.wa.bill.exception.BillException;
import com.wow.wa.bill.serivce.BillService;
import com.wow.wa.bill.serivce.BillServiceImpl;
import com.wow.wa.bill.util.Args;
import com.wow.wa.bill.vo.Bill;
import com.wow.wa.bill.vo.Card;
import com.wow.wa.bill.vo.Category;
import com.wow.wa.ui.CategoryBtn;
import com.wow.wa.ui.DialogFactory;
import com.wow.wa.ui.bean.UICategory;
import com.wow.wa.util.CommonUtils;

public class AddBillActivity extends BaseActivity implements OnClickListener {

	private static final String TAG = AddBillActivity.class.getSimpleName();

	private BillService service;

	private CategoryBtn dateBtn = null;
	private CategoryBtn name;// 名稱
	private CategoryBtn dollar;// 金額
	private CategoryBtn note;// 備註

	private CategoryBtn isPayed;// 是否付費

	private Button saveBtn;
	private Button updateBtn;

	private NotifyComponet notifyComponet;

	@SuppressWarnings("unchecked")
	@Override
	public Class<? extends BaseActivity> toWhenError() {
		return (Class<? extends BaseActivity>) com.wo.android.pockybill.util.Args.Activity.homeActivity;
	}

	private List<UICategory> toCategory(List<Category> categorys) {
		final List<UICategory> uis = new ArrayList<UICategory>();
		for (Category category : categorys) {
			UICategory ui = new UICategory();
			ui.setCode(category.get_id() + "");
			ui.setName(category.getName());
			uis.add(ui);
		}
		return uis;
	}

	private List<UICategory> getIsPays() {
		final List<UICategory> isPayCategorys = new ArrayList<UICategory>();
		isPayCategorys.add(new UICategory(Args.Common.STRING_Y, this
				.getString(R.string.yes)));
		isPayCategorys.add(new UICategory(Args.Common.STRING_N, this
				.getString(R.string.no)));
		return isPayCategorys;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.add_one_view);

		final String title = this.getIntent().getExtras()
				.getString(Args.Common.PAY_TYPE);
		this.getActionBar().setTitle(title);
		this.getActionBar().setDisplayHomeAsUpEnabled(true);

		notifyComponet = new NotifyComponetImpl(this);

		// --------------------------------------------------------------//
		// view init
		// --------------------------------------------------------------//

		this.note = (CategoryBtn) this.findViewById(R.id.note);
		this.name = (CategoryBtn) this.findViewById(R.id.name);
		this.dollar = (CategoryBtn) this.findViewById(R.id.dollar);
		this.saveBtn = (Button) this.findViewById(R.id.saveBtn);
		this.updateBtn = (Button) this.findViewById(R.id.updateBtn);

		this.isPayed = (CategoryBtn) this.findViewById(R.id.isPayed);
		this.dateBtn = (CategoryBtn) this.findViewById(R.id.dateBtn);

		// --------------------------------------------------------------//
		// service init
		// --------------------------------------------------------------//
		this.service = new BillServiceImpl(this);
		this.dateBtn.crateDateDialog(StringUtils.EMPTY);
		final Card card = (Card) this.getIntent().getSerializableExtra(
				Args.Common.CARD);

		if (StringUtils.equals(card.getType(), Args.Common.STRING_ONE)) {

			this.findViewById(R.id.duteLayout).setVisibility(View.GONE);
			this.findViewById(R.id.buyDateLayout).setVisibility(View.VISIBLE);

		} else if (StringUtils.equals(card.getType(), Args.Common.STRING_ZERO)) {
			this.findViewById(R.id.duteLayout).setVisibility(View.VISIBLE);
			this.findViewById(R.id.buyDateLayout).setVisibility(View.GONE);
		}

		// --------------------------------------------------------------//
		// categoryBtn init
		// --------------------------------------------------------------//

		this.name.createEditDialog(this.getString(R.string.name),
				StringUtils.EMPTY, false, InputType.TYPE_CLASS_TEXT);

		this.dollar.createEditDialog(this.getString(R.string.allMoney),
				StringUtils.EMPTY, false, InputType.TYPE_CLASS_NUMBER);

		this.isPayed.createCategoryDialog(this.getString(R.string.isPay),
				this.getIsPays(), true, StringUtils.EMPTY, StringUtils.EMPTY);

		this.note.createEditDialog(this.getString(R.string.note),
				StringUtils.EMPTY, false, InputType.TYPE_CLASS_TEXT);

		this.isPayed.bindOnClickEvent();
		this.name.bindOnClickEvent();
		this.dollar.bindOnClickEvent();
		this.note.bindOnClickEvent();
		this.dateBtn.bindOnClickEvent();

		// --------------------------------------------------------------//

		this.updateBtn.setOnClickListener(this);
		this.saveBtn.setOnClickListener(this);

		this.saveBtn.setVisibility(View.VISIBLE);
		this.updateBtn.setVisibility(View.GONE);

		this.isPayed.setCode(Args.Common.STRING_N);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return true;
	}

	/**
	 * 修改
	 */
	private void saveBill() {
		try {
			final Card card = (Card) this.getIntent().getSerializableExtra(
					Args.Common.CARD);

			final Bill bill = new Bill();

			bill.setName(name.getBeanValue());
			bill.setDollar(dollar.getBeanValue());
			bill.setDueDate(this.dateBtn.getBeanValue());
			bill.setIsShowPayed(this.isPayed.getBeanValue());
			bill.setNote(this.note.getBeanValue());
			bill.setType(card.get_id() + "");

			bill.setIsPayed(this.isPayed.getBeanValue());

			if (StringUtils.isBlank(bill.getName())) {
				throw new BillException(this.getString(R.string.plsEnter)
						+ name.getTitle());
			}
			if (StringUtils.isBlank(bill.getIsPayed())) {
				throw new BillException(this.getString(R.string.isPayed)
						+ name.getTitle());
			}

			this.service.insertBill(bill, this);
		} catch (BillException ex) {
			throw ex;

		}

		catch (Exception e) {
			Log.v(TAG, "updateBill e" + e);

		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:

			this.finish();

			break;

		default:
			break;
		}
		return true;
	}

	@Override
	public void onClick(View v) {

		if (v == this.updateBtn) {
			// this.updateBill();
			AddBillActivity.this.setResult(Args.Common.EDIT_BILL_CODE);

			this.finish();
		} else if (v == saveBtn) {
			try {
				this.saveBill();
				AddBillActivity.this.setResult(Args.Common.EDIT_BILL_CODE);
				this.finish();
			} catch (BillException e) {
				DialogFactory.getWarm(e.getName(), this).show();
			}
		}

	}
}
